package http_servicer

import (
	"log"
	"net/http"
	"os"

	"github.com/gorilla/mux"
	"github.com/gorilla/securecookie"
)

var version string

var cookieHandler = securecookie.New(
	securecookie.GenerateRandomKey(64),
	securecookie.GenerateRandomKey(32))

// ..
/*
type retMsg struct {
	Flag    int8
	Message string
	Data    []map[string]string
}
*/
type simple_retMsg struct {
	Flag    int8
	Message string
}

var router = mux.NewRouter()

func MuxRouter() *mux.Router {
	return router
}

var gUser string

//var CookieHandler = cookieHandler

var logger *log.Logger

func init() {

	chan2Device = make(chan string, 10)
	logger = log.New(os.Stdout, "HTTP SVR>", log.LstdFlags|log.Lshortfile)
	gUser = os.Getenv("DUSER")
	//  This Handler is for test purpose only ****************************************
	router.HandleFunc("/test", testPageHandler)
	// ********************************************************************************

	router.HandleFunc("/", indexPageHandler)
	router.HandleFunc("/iternal", internalPageHandler)
	router.HandleFunc("/version", versionHandler)
	///  user_auth.go  ----------------------------------------------------
	router.HandleFunc("/login", loginHandler).Methods("POST")
	router.HandleFunc("/logout", logoutHandler).Methods("POST")
	router.HandleFunc("/chgpwd", chgpwdHandler).Methods("POST")
	router.HandleFunc("/register", registerHandler).Methods("POST")
	router.HandleFunc("/loginstatus", loginstatusHandler).Methods("POST")
	router.HandleFunc("/fetch_child_addr", fetchChildAddrHandler).Methods("POST")
	router.HandleFunc("/fetch_user_info", fetchUserInfoHandler).Methods("POST")
	router.HandleFunc("/modifyaddr", modiUserAddrHandler).Methods("POST")
	router.HandleFunc("/modify_user_alert_num", modifyUserAlertNumHandler).Methods("POST")
	router.HandleFunc("/fetch_continents", fetchContinentsHandler).Methods("POST")
	router.HandleFunc("/fetch_countries", fetchCountriesHandler).Methods("POST")

	///  device_info.go  --------------------------------------------
	router.HandleFunc("/newdevice", newdeviceHandler).Methods("POST")
	router.HandleFunc("/modifydevice", modifydeviceHandler).Methods("POST")
	router.HandleFunc("/devicelist", devicelistHandler).Methods("POST")

	///  pond_info.go  --------------------------------------------
	router.HandleFunc("/newpond", newpondHandler).Methods("POST")
	router.HandleFunc("/fetchpond", fetchpondHandler).Methods("POST")
	router.HandleFunc("/modifypond", modifypondHandler).Methods("POST")
	router.HandleFunc("/revokepond", revokepondHandler).Methods("POST")

	/// device_data.go  --------------------------------------
	router.HandleFunc("/realtimepondinfo", realtimepondinfoHandler).Methods("POST")

	router.HandleFunc("/set_device_margin", setDeviceMarginHandler).Methods("POST")
	router.HandleFunc("/get_device_margin", getDeviceMarginHandler).Methods("POST")
	router.HandleFunc("/cmd_device_align", cmdDeviceAlignHandler).Methods("POST")
	router.HandleFunc("/fetch_device_align", fetchDeviceAlignHandler).Methods("POST")

	router.HandleFunc("/pondhistdatalist", pondHistDataListHandler).Methods("POST")
	router.HandleFunc("/pondhistdatadaily", pondHistDataDailyHandler).Methods("POST")
	router.HandleFunc("/pondhistdatahourly", pondHistDataHourlyHandler).Methods("POST")

	/*  ---- un-implemented interface --------------------

	modify_user_alert_num

	pondinfohist

	 ---- un-implemented interface --------------------
	*/

	http.Handle("/", router)

	go send2Device()

}
